package com.ae.shield.common.items.util;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;

import javax.annotation.Nonnull;

//可用于仅在gui内有物品互动的物品
public class ItemInventory extends Inventory {
    private final String TAG_ITEMS = "Items";
    private final ItemStack stack;
    private final int size;

    public ItemInventory(ItemStack stackIn, int sizeIn) {
        super(sizeIn);
        size = sizeIn;
        stack = stackIn;
        readItems();
    }

    private void readItems() {
        ListNBT listNBT = ItemNBTHelper.getList(stack, TAG_ITEMS, 10, new ListNBT());
        for (int i = 0; i < listNBT.size(); i++) {
            setInventorySlotContents(i, ItemStack.read(listNBT.getCompound(i)));
        }
    }

    @Override
    public boolean isUsableByPlayer(@Nonnull PlayerEntity playerIn) {
        return !stack.isEmpty();
    }

    @Override
    public void markDirty() {
        super.markDirty();
        ListNBT nbtTagList = new ListNBT();
        for (int i = 0; i < size; i++) {
            nbtTagList.add(getStackInSlot(i).write(new CompoundNBT()));
        }
        ItemNBTHelper.setList(stack, TAG_ITEMS, nbtTagList);
    }

    //配合以下内容使用
//    private static class InventoryProvider implements ICapabilityProvider {
//        private final LazyOptional<IItemHandler> optional;
//        protected final IInventory inventory;
//
//        public InventoryProvider(ItemStack stackIn) {
//            inventory = getInventory(stackIn);
//            optional = LazyOptional.of(() -> new InvWrapper(inventory));
//        }
//
//        @Nonnull
//        @Override
//        public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
//            return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.orEmpty(cap, optional);
//        }
//    }
//
//    public static IInventory getInventory(ItemStack stack) {
//        return new ItemInventory(stack, slotSize) {
//            @Override
//            public boolean isItemValidForSlot(int slot, ItemStack stack) {
//                return stack.getItem() instanceof ShieldMakerBase;
//            }
//        };
//    }
}
